package fr.iut2.sil1.trolledit.client;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
* NumberSpinner Custom Control
* 
* @author Pavan Andhukuri
* 
*/
public class NumberSpinner extends Composite {

    private int RATE = 1;
    private TextBox integerBox;

    public NumberSpinner() {
        this(0, "");
    }

    public NumberSpinner(int defaultValue, String typeCarac) {
        HorizontalPanel hPanel = new HorizontalPanel();
        hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        initWidget(hPanel);

        String down = new String("-");
        Button downButton = new Button(down);
        downButton.setStyleName("dp-spinner-downbutton "+typeCarac);
        downButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            	String typeCa = ((Button) event.getSource()).getStyleName().split(" ")[1];
            	Boolean bool = Caracteristique.getSINGLETONCARAC().updateMoinsPtsRestants(typeCa, getValue());
                if(bool){
	            	if (getValue() == 0)
	                    return;
	            	 setValue(getValue() - RATE);
                }else{
                	Window.alert("Vous ne pouvez pas enlever plus de points de competences!!");
                }
                
            }
        });
        
        hPanel.add(downButton);

        integerBox = new TextBox();
        integerBox.setEnabled(false);
        hPanel.add(integerBox);
        integerBox.setSize("50px", "20px");
        String defaultValueS = ""+defaultValue;
        integerBox.setText(defaultValueS);  
       
        String up = new String("+");
        Button upButton = new Button(up);
        upButton.setStyleName("dp-spinner-downbutton "+typeCarac);
        upButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            	String typeCa = ((Button) event.getSource()).getStyleName().split(" ")[1];
            	Boolean bool = Caracteristique.getSINGLETONCARAC().updatePlusPtsRestants(typeCa, getValue());
                if(bool){
	                if(getValue() == 20 && typeCa.equals("CaracSpe"))
	                		return;
	                if(getValue() == 50 && typeCa.equals("Carac"))
                		return;
	                setValue(getValue() + RATE);
                }else{
                	Window.alert("Vous ne pouvez pas ajouter plus de points de competences!!");
                }
            }
        });
        hPanel.add(upButton);
       // upButton.setSize("30px", "20px");
    }

    /**
     * Returns the value being held.
     * 
     * @return
     */
    public int getValue() {
        if (integerBox.getText() == "")
			return 0;
		else
			return Integer.parseInt(integerBox.getText());
    }

    /**
     * Sets the value to the control
     * 
     * @param value
     *            Value to be set
     */
    public void setValue(int value) {
        String valueS = ""+value;
    	integerBox.setText(valueS);
    }

    /**
     * Sets the rate at which increment or decrement is done.
     * 
     * @param rate
     */
    public void setRate(int rate) {
        this.RATE = rate;
    }
}